package com.yal.controller;

import com.alibaba.fastjson.JSON;
import com.yal.domian.Order;
import com.yal.domian.Product;
import com.yal.service.ProductService;
import com.yal.service.impl.OrderServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class OrderController {

    @Autowired
    private OrderServiceImpl osi;
//
//    @Autowired
//    private DiscoveryClient discoveryClient; //调用微服务 母系社会方法
//
//    @Autowired
//    private RestTemplate restTemplate;  //调用微服务 原始人方法

    @Autowired
    private ProductService productService;

    @RequestMapping(value = "/XiaOrder/{pid}",produces = "application/json;charset=UTF-8")
    public Order XiaOrder(@PathVariable("pid") Integer pid) {
        log.info("准备创建订单");
        log.info("准备调用商品服务");
        Product product = productService.findByPid(pid);
        log.info("调用成功，查询结果为：{}", JSON.toJSONString(product));
        if (product.getPid() == -100){
            Order order = new Order();
            order.setId(-100);
            order.setPname("下单失败");
            return order;
        }
        //创建订单
        Order order = new Order();
        order.setPid(pid);
        order.setUid(1);
        order.setUsername("吴彦祖");
        order.setNumber(10);
        order.setPname(product.getName());
        order.setPprice(product.getPrice());

        osi.insertOrder(order);

        return order;
    }

}